<html>
<head>
<title>Todo</title>
<link rel="icon" type="image/png" href="../apple-touch-icon.png"/>
<style type="text/css">
body{font-family:"Lucida Grande",Arial, Helvetica, sans-serif; font-size:14px; color:#222222;}
li.done {color: #999;}
li.add {margin: 0 0 10 5;}
ul li {list-style-type: none;}
li {  height: 25px; }
input { vertical-align: -15%; } 
h1 {margin: 0 0 0 43;}

</style>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
{
location.replace("iphone.php");
}



var xmlHttp;

function addItem(Add){
		var Add = "add="+Add;
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
         {
         alert ("Browser does not support HTTP Request");
         return;
         }
        var url="index.php";
        xmlHttp.open("POST",url,true);
        
        //Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", Add.length);
		xmlHttp.setRequestHeader("Connection", "close");
        
        xmlHttp.onreadystatechange=stateChanged;
        xmlHttp.send(Add);
        return false;
}


function ajaxFunction(Checker,Bool){ 
		var CheckUrl = "completed="+Checker+"&bool="+Bool;
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
         {
         alert ("Browser does not support HTTP Request");
         return;
         }
        var url="index.php";
        xmlHttp.open("POST",url,true);
        
        //Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", CheckUrl.length);
		xmlHttp.setRequestHeader("Connection", "close");
        
        xmlHttp.onreadystatechange=stateChanged;
        xmlHttp.send(CheckUrl);
}



function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("ajaxDiv").innerHTML=xmlHttp.responseText;
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}





//-->
</script>

<div id="ajaxDiv">
<?php
include('dbconnection.php');

        // Retrieve data from Query String
$done = $_POST['completed'];
$bool = $_POST['bool'];
$add = $_POST['add'];
        // Escape User Input to help prevent SQL Injection
$done = mysql_real_escape_string($done);
$bool = mysql_real_escape_string($bool);
$add = mysql_real_escape_string($add);
        //build query



if($done){
	if($bool == 1){$yesno = 0;} else {$yesno = 1;}
	$now	=	date('Y-m-d H:i:s');
	$query = "UPDATE TODOS SET done = $yesno , done_date = '$now' WHERE id_num = $done";
	$qry_result = mysql_query($query) or die(mysql_error());
}

if($add){
	$now	=	date('Y-m-d H:i:s');
	$query = "INSERT INTO TODOS (todo,done,add_date) VALUES('$add','0','$now')";
	$qry_result = mysql_query($query) or die(mysql_error());
}



        //get the number of todos
$query = "SELECT * FROM TODOS where done = 0";
$qry_result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($qry_result);

        //Build Result String
echo "<h1>".$num_rows." Todos</h1>\n";
echo "<ul>\n";

#$query = "SELECT * FROM TODOS ORDER by done ASC, add_date DESC";
$query = "Select * FROM TODOS WHERE done = 0 OR (done_date > DATE_SUB(NOW(), INTERVAL 29 DAY) AND done = 1) ORDER BY done ASC, add_date DESC;";
$qry_result = mysql_query($query) or die(mysql_error());


echo "<li class='add'><form name='formadd' onsubmit='return addItem(formadd.add.value);' autocomplete='off'><input name='add' id='add' type='text' size='40'> <input type='button' id='addit' name='button' value='Add' onclick='addItem(form.add.value);'></form></li>\n";


        // Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
	echo "<li";
	if ($row[done] == 1){echo " class='done'";}
	echo ">";
	echo "<form name='myForm".$row[id_num]."'><input type='checkbox' name='dope".$row[id_num]."' value='1' onclick='ajaxFunction(".$row[id_num].",".$row[done].")'";
	if ($row[done] == 1){echo " checked";}
	echo "><input name='done".$row[id_num]."' id='done' type='hidden' value='";
	echo $row[id_num];
	echo "' />";
	echo $row[todo];
	echo "</form></li>\n";
}
echo "</ul>";
?>


</div>
</body>
</html>